home *** CD-ROM | disk | FTP | other *** search
/ Visual Cafe 3 / Visual Cafe 3.ISO / Vcafe / JFC.bin / ProgressMonitor.java < prev    next >
Text File  |  1998-06-30  |  7KB  |  177 lines

  1. /*
  2.  * @(#)ProgressMonitor.java    1.10 98/02/02
  3.  * 
  4.  * Copyright (c) 1997 Sun Microsystems, Inc. All Rights Reserved.
  5.  * 
  6.  * This software is the confidential and proprietary information of Sun
  7.  * Microsystems, Inc. ("Confidential Information").  You shall not
  8.  * disclose such Confidential Information and shall use it only in
  9.  * accordance with the terms of the license agreement you entered into
  10.  * with Sun.
  11.  * 
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
  13.  * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  14.  * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  15.  * PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR ANY DAMAGES
  16.  * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING
  17.  * THIS SOFTWARE OR ITS DERIVATIVES.
  18.  * 
  19.  */
  20.  
  21. package com.sun.java.swing;
  22.  
  23. import java.io.*;
  24. import java.awt.Frame;
  25. import java.awt.Component;
  26.  
  27. /** A class to monitor the progress of some operation.  If it looks
  28.  * like it will take a while, a progress dialog will be popped up.
  29.  * When the ProgressMonitor is created it is given a numeric range and a descriptive
  30.  * string.  As the operation progresses, call the setProgress method
  31.  * to indicate how far along the [min,max] range the operation is.
  32.  * Initially, there is no ProgressDialog.  After the first millisToDecideToPopup
  33.  * milliseconds (default 500) the progress monitor will predict how long
  34.  * the operation will take.  If it is longer than millisToPopup (default 2000,
  35.  * 2 seconds) a ProgressDialog will be popped up.
  36.  * <p>
  37.  * From time to time, when the Dialog box is visible, the progress bar will
  38.  * be updated when setProgress is called.  setProgress won't always update
  39.  * the progress bar, it will only be done if the amount of progress is
  40.  * visibly significant.
  41.  * <p>
  42.  * @see ProgressMonitorInputStream
  43.  * @author James Gosling
  44.  */
  45. public class ProgressMonitor {
  46.     private int millisToDecideToPopup = 500;
  47.     private int millisToPopup = 2000;
  48.     private int min, max, v, lastDisp;
  49.     private int reportDelta;
  50.     private ProgressMonitor root;
  51.     private JDialog dialog;
  52.     private JOptionPane pane;
  53.     private JProgressBar myBar;
  54.     private JLabel noteLabel;
  55.     private Component parentComponent;
  56.     private String note;
  57.     private Object message;
  58.     private long T0;
  59.  
  60.     /**
  61.     @param parentComponent the parent component for the dialog box
  62.     @param message a descriptive message that will be shown
  63.         to the user to indicate what operation is being monitored.
  64.         This does not change as the operation progresses.
  65.         See the message parameters to methods in
  66.         <a href=com.sun.java.swing.JOptionsPane.html#message>JOptionsPane</a>
  67.         for the range of values.
  68.     @param note a short note describing the state of the
  69.         operation.  As the operation progresses, you can call
  70.         setNote to change the note displayed.  This is used,
  71.         for example, in operations that iterate through a
  72.         list of files to show the name of the file being processes.
  73.         If note is initially null, there will be no note line
  74.         in the dialog box and setNote will be ineffective
  75.     @param min the lower bound of the range
  76.     @param max the upper bound of the range
  77.     @see JDialog
  78.     @see JOptionPane
  79.      */
  80.     public ProgressMonitor(Component parentComponent, Object message, String note, int min, int max) {
  81.         this(parentComponent,message,note,min,max,null);
  82.     }
  83.     /** Some day I want to put in groups of progress monitors... */
  84.     private ProgressMonitor(Component parentComponent, Object message, String note,
  85.                 int min, int max, ProgressMonitor group) {
  86.         this.min = min;
  87.         this.max = max;
  88.         this.parentComponent = parentComponent;
  89.         reportDelta = (max-min)/100;
  90.         if (reportDelta<1) reportDelta = 1;
  91.         v = min;
  92.         this.message = message;
  93.     this.note = note;
  94.         if (group != null) {
  95.             root = group.root != null ? group.root : group;
  96.             T0 = root.T0;
  97.             dialog = root.dialog;
  98.         } else {
  99.             T0 = System.currentTimeMillis();
  100.         }
  101.     }
  102.     private static class ProgressOptionPane extends JOptionPane {
  103.     ProgressOptionPane(Object messageList) {
  104.         super(messageList, JOptionPane.INFORMATION_MESSAGE,
  105.           JOptionPane.OK_CANCEL_OPTION, null, null, null);
  106.     }
  107.     public int getMaxCharactersPerLineCount() { return 60; }
  108.     };
  109.     /** Indicate the progress of the operation being monitored. */
  110.     public void setProgress(int nv) {
  111.         v = nv;
  112.         if (nv >= max) close();
  113.         else if (nv>=lastDisp+reportDelta) {
  114.             lastDisp = nv;
  115.             if(myBar != null) myBar.setValue(nv);
  116.             else {
  117.                 long T = System.currentTimeMillis();
  118.                 long dT = (int)(T-T0);
  119.                 if (dT >= millisToDecideToPopup) {
  120.                     int predictedCompletionTime;
  121.                     if (nv>min)
  122.                     predictedCompletionTime = (int)((long)dT*(max-min)/(nv-min));
  123.                     else predictedCompletionTime = millisToPopup;
  124.                     if (predictedCompletionTime>=millisToPopup) {
  125.                         myBar = new JProgressBar();
  126.             myBar.setMinimum(min);
  127.             myBar.setMaximum(max);
  128.             myBar.setValue(nv);
  129.             if (note != null) noteLabel = new JLabel(note);
  130.             pane = new ProgressOptionPane(new Object[]{message, noteLabel, myBar});
  131.             dialog = pane.createDialog(parentComponent, "Progress...");
  132.             dialog.setModal(false);
  133.             dialog.show();
  134.                     }
  135.                 }
  136.             }
  137.         }
  138.     }
  139.     /** Indicate that the operation is complete.  This happens automatically
  140.      * when the value set by setProgress is >= max, but it may be called
  141.      * earlier if the operation ends early. */
  142.     public void close() {
  143.         if (dialog != null) {
  144.             dialog.setVisible(false);
  145.             dialog.dispose();
  146.             dialog = null;
  147.         pane = null;
  148.             myBar = null;
  149.         }
  150.     }
  151.     public int getMinimum() { return min; }
  152.     public void setMinimum(int m) { min = m; }
  153.     public int getMaximum() { return max; }
  154.     public void setMaximum(int m) { max = m; }
  155.     /** Returns true if the user does some UI action to cancel this operation.
  156.      * (like hitting the Cancel button on the progress dialog). */
  157.     public boolean isCanceled() {
  158.     if (pane == null) return false;
  159.     Object v = pane.getValue();
  160.     return v!=null && v instanceof Integer && ((Integer)v).intValue()==2;
  161.     }
  162.     public void setMillisToDecideToPopup(int millisToDecideToPopup) {
  163.         this.millisToDecideToPopup = millisToDecideToPopup;
  164.     }
  165.     public int getMillisToDecideToPopup() { return millisToDecideToPopup; }
  166.     public void setMillisToPopup(int millisToPopup) {
  167.         this.millisToPopup = millisToPopup;
  168.     }
  169.     public int getMillisToPopup() { return millisToPopup; }
  170.     public void setNote(String note) {
  171.         this.note = note;
  172.         if (noteLabel != null)
  173.             noteLabel.setText(note);
  174.     }
  175.     public String getNote() { return note; }
  176. }
  177.